<?php
// +----------------------------------------------------------------------
// | 中犇单商户
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2021 中犇科技 All rights reserved.
// +----------------------------------------------------------------------


namespace app\user\admin;

use app\admin\admin\Base;
use app\user\model\Level as LevelModel;
use service\Format;
use think\Db;

/**
 * 会员等级控制器
 * @package app\Level\admin
 */
class Level extends Base
{
    /**
     * 会员等级列表
     * @return mixed
     * @author 似水星辰 [2630481389@qq.com]
     */
    public function index()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        // 查询
//        $map = $this->getMap();
        $type = input('type');
        if ($type) {
            $map = ['type' => $type];
        } else {
            $map = true;
        }

        // 排序
        $order = 'level';
        // 数据列表
        $data_list = LevelModel::where($map)->order($order)->paginate()->each(function (&$v) {
            $v['bg_image'] = get_file_url($v['bg_image']);
            return $v;
        });
        $search_fields = [
            ['type', '类型', 'select', '', [0 => '全部', '1' => '月卡', '2' => '季月', '3' => '年卡']],
        ];
        $fields = [
            ['id', 'ID编号'],
            ['level_name', lang('名称')],
            ['level', lang('等级标识')],
            ['bg_image', lang('背景图片'), 'picture'],
            ['market_price', lang('划线价')],
            ['price', lang('价格')],
            ['right_button', lang('操作'), 'btn', '', '', 'text-center']
        ];
        return Format::ins()//实例化
        ->hideCheckbox()
            ->addColumns($fields)//设置字段
//            ->setTopSearch($search_fields)
            ->setTopButtons($this->top_button)
            ->setRightButtons($this->right_button)
            ->setData($data_list)//设置数据
            ->fetch();//显示
    }

    /**
     * 新增
     * @return mixed
     * @author 似水星辰 [2630481389@qq.com]
     */
    public function add()
    {
        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, \app\user\validate\Level::class);
            if (true !== $result) {
                $this->error($result);
            }
            if ($page = LevelModel::create($data)) {
                $this->success(lang('新增成功'), cookie('__forward__'));
            } else {
                $this->error(lang('新增失败'));
            }
        }
        $fields = [
            ['type' => 'number', 'name' => 'level', 'title' => lang('等级标识'), 'tips' => '', 'attr' => '', 'value' => ''],
            ['type' => 'text', 'name' => 'level_name', 'title' => lang('名称'), 'tips' => '', 'attr' => '', 'value' => ''],
            ['type' => 'image', 'name' => 'bg_image', 'title' => lang('会员展示背景图'), 'tips' => '', 'attr' => '', 'value' => ''],
            ['type' => 'image', 'name' => 'vip_image', 'title' => lang('会员图标'), 'tips' => '会员小图标', 'attr' => '', 'value' => ''],
            ['type' => 'text', 'name' => 'market_price', 'title' => lang('划线价'), 'tips' => '', 'attr' => '', 'value' => ''],
            ['type' => 'text', 'name' => 'price', 'title' => lang('实际价格'), 'tips' => '', 'attr' => '', 'value' => ''],
//            ['type' => 'text', 'name' => 'discount', 'title' => lang('折扣（0.00-1.00）'), 'tips' => '下单购买商品时折扣比例', 'attr' => '', 'value' => ''],
//            ['type' => 'number', 'name' => 'expire', 'title' => lang('天数'), 'tips' => '购买后会员有效天数', 'attr' => '', 'value' => '0'],
            ['type' => 'textarea', 'name' => 'content', 'title' => lang('权限说明'), 'tips' => '', 'attr' => '', 'value' => ''],
//            ['type' => 'number', 'name' => 'vip_score', 'title' => lang('购买会员奖励积分'), 'tips' => '购买会员下单奖励积分', 'attr' => '', 'value' => '0'],
//            ['type' => 'number', 'name' => 'post_score', 'title' => lang('发帖奖励积分'), 'tips' => '穿搭社区发帖奖励积分', 'attr' => '', 'value' => '0'],
//            ['type' => 'money', 'name' => 'order_score', 'title' => lang('下单奖励积分'), 'tips' => '购买商品下单奖励积分(按照实付金额比例奖励积分)', 'attr' => '', 'value' => '0.00'],
//            ['type' => 'number', 'name' => 'comment_score', 'title' => lang('下单评论奖励积分'), 'tips' => '下单发布评论奖励积分', 'attr' => '', 'value' => '0'],
//            ['type' => 'number', 'name' => 'position', 'title' => lang('对应衣位数量'), 'tips' => '对应衣位数量', 'attr' => '', 'value' => '0'],
            ['type' => 'color', 'name' => 'color', 'title' => lang('字体颜色'), 'tips' => '', 'attr' => '', 'value' => ''],
            ['type' => 'text', 'name' => 'direct', 'title' => lang('直推佣金比例（0.00-1.00）'), 'tips' => '直推奖励佣金比例', 'attr' => '', 'value' => '0.5'],
            ['type' => 'text', 'name' => 'indirect', 'title' => lang('间推佣金比例（0.00-1.00）'), 'tips' => '间推奖励佣金比例', 'attr' => '', 'value' => '0.5'],
        ];
        $this->assign('page_title', lang('新增会员等级'));
        $this->assign('form_items', $fields);
        return $this->fetch('../../admin/view/public/add');
    }

    /**
     * 编辑
     * @param null $id 会员等级id
     * @return mixed
     * @author 似水星辰 [2630481389@qq.com]
     */
    public function edit($id = null)
    {
        if ($id === null) {
            $this->error(lang('缺少参数'));
        }
        $info = LevelModel::get($id);
        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, \app\user\validate\Level::class);
            if (true !== $result) {
                $this->error($result);
            }
            if (LevelModel::update($data)) {
                $cacheName = "user:level:{$info['level']}";
                cache($cacheName, null);//编辑等级信息的时候清空缓存
                $this->success(lang('编辑成功'), cookie('__forward__'));
            } else {
                $this->error(lang('编辑失败'));
            }
        }
        $fields = [
            ['type' => 'hidden', 'name' => 'id'],
            ['type' => 'number', 'name' => 'level', 'title' => lang('等级标识'), 'tips' => '', 'attr' => '', 'value' => ''],
            ['type' => 'text', 'name' => 'level_name', 'title' => lang('名称'), 'tips' => '', 'attr' => '', 'value' => ''],
            ['type' => 'image', 'name' => 'bg_image', 'title' => lang('会员背景图'), 'tips' => '', 'attr' => '', 'value' => ''],
            ['type' => 'image', 'name' => 'vip_image', 'title' => lang('会员图标'), 'tips' => '会员小图标', 'attr' => '', 'value' => ''],
            ['type' => 'text', 'name' => 'market_price', 'title' => lang('划线价'), 'tips' => '', 'attr' => '', 'value' => ''],
            ['type' => 'text', 'name' => 'price', 'title' => lang('实际价格'), 'tips' => '', 'attr' => '', 'value' => ''],
//            ['type' => 'text', 'name' => 'discount', 'title' => lang('折扣（0.00-1.00）'), 'tips' => '下单购买商品时折扣比例', 'attr' => '', 'value' => ''],
//            ['type' => 'number', 'name' => 'expire', 'title' => lang('天数'), 'tips' => '购买后会员有效天数', 'attr' => '', 'value' => '0'],
            ['type' => 'wangeditor', 'name' => 'content', 'title' => lang('权限说明'), 'tips' => '', 'attr' => '', 'value' => ''],
//            ['type' => 'number', 'name' => 'vip_score', 'title' => lang('购买会员奖励积分'), 'tips' => '购买会员下单奖励积分', 'attr' => '', 'value' => '0'],
//            ['type' => 'number', 'name' => 'post_score', 'title' => lang('发帖奖励积分'), 'tips' => '穿搭社区发帖奖励积分', 'attr' => '', 'value' => '0'],
//            ['type' => 'money', 'name' => 'order_score', 'title' => lang('下单奖励积分'), 'tips' => '购买商品下单奖励积分(按照实付金额比例奖励积分)', 'attr' => '', 'value' => '0.00'],
//            ['type' => 'number', 'name' => 'comment_score', 'title' => lang('下单评论奖励积分'), 'tips' => '下单发布评论奖励积分', 'attr' => '', 'value' => '0'],
//            ['type' => 'number', 'name' => 'position', 'title' => lang('对应衣位数量'), 'tips' => '对应衣位数量', 'attr' => '', 'value' => '0'],
            ['type' => 'color', 'name' => 'color', 'title' => lang('字体颜色'), 'tips' => '', 'attr' => '', 'value' => ''],
            ['type' => 'text', 'name' => 'direct', 'title' => lang('直推佣金比例（0.00-1.00）'), 'tips' => '直推奖励佣金比例', 'attr' => '', 'value' => ''],
            ['type' => 'text', 'name' => 'indirect', 'title' => lang('间推佣金比例（0.00-1.00）'), 'tips' => '间推奖励佣金比例', 'attr' => '', 'value' => ''],
        ];
        $this->assign('page_title', lang('编辑会员等级'));
        $this->assign('form_items', $this->setData($fields, $info));
        return $this->fetch('../../admin/view/public/edit');
    }

    /**
     *
     */
    public function delete_card_data()
    {
        // 启动事务
        Db::startTrans();
        try {
            $res = Db::name('user_level_card_data')->where('id', '<>', 0)->update(['status' => 0]);
            if (!$res) {
                exception(lang('更新0条数据'));
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success(lang('清除成功'));
        return $this->index();
    }
}
